import sqlalchemy as sa
import json
from MyConfig import MyConfig

if __name__=="__main__":
    try:
        mysql_prod= "mariadb+pymysql://uupadmin:uupadmin@vsys01775:3306"
        engine = sa.create_engine(mysql_prod)
        sql = sa.text("SELECT name, code FROM consolidation.handlers")
        cfg = MyConfig("consolidation.json")
        hdict:dict = cfg.get("level2 handlers",None)
        with engine.connect() as conn:
            for rec in conn.execute(sql):
                rdict = rec._asdict()
                with open(f"handlers2\\{hdict[rdict['name']]}.py",mode="w",encoding="utf-8") as f:
                    f.write(rdict["code"])
                    f.flush()
                    f.close()
                print(f"{rdict['name']} : {hdict[rdict['name']]} - done")
            conn.close()
    except Exception as exp:
        print(f"error: {exp}")
